#ifndef HALCONWIDGET_H
#define HALCONWIDGET_H

#include <QWidget>
#include "halconcpp/HalconCpp.h"

class HalconWidget : public QWidget
{
    Q_OBJECT
public:
    explicit HalconWidget(QWidget *parent = nullptr);

    ~HalconWidget();

    // 图像显示相关方法
    void displayImage(const HalconCpp::HObject &image);
    void clearDisplay();
    void fitImageToWindow();
    void setPart(double row1, double col1, double row2, double col2);

    // 获取Halcon窗口句柄
    HalconCpp::HTuple getWindowHandle() const { return hWindowID; }

    // 缩放控制
    void setZoom(double factor);
    void zoomIn();
    void zoomOut();
    void resetZoom();

public slots:
    void onDisplayImage(const QString& filename);

protected:
    // 重写Qt事件处理函数
    void paintEvent(QPaintEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;

private:
    void initializeHalconWindow();
    void updateImageDisplay();

private:
    HalconCpp::HTuple hWindowID;      // Halcon窗口句柄
    HalconCpp::HObject currentImage;   // 当前显示的图像
    bool imageLoaded;                 // 图像加载标志

    // 鼠标交互相关变量
    QPoint lastMousePos;
    bool isDragging;
    double zoomFactor;

    // 图像显示区域
    double imageRow1, imageCol1, imageRow2, imageCol2;
};

#endif // HALCONWIDGET_H
